﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using CFZS.IDAL;
using CFZS.DALFactory;
using CFZS.WebHelper;

namespace CFZS.BLL
{
    /// <summary>
    /// BaseDictionary
    /// </summary>
    public partial class BaseDictionary
    {
        private readonly IBaseDictionary dal = DataAccess.CreateBaseDictionary();
        public BaseDictionary()
        { }
        #region  Method
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int Id)
        {
            return dal.Exists(Id);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(CFZS.Model.BaseDictionary model)
        {
            return dal.Add(model);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(CFZS.Model.BaseDictionary model)
        {
            return dal.Update(model);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int Id)
        {

            return dal.Delete(Id);
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool DeleteList(string Idlist)
        {
            return dal.DeleteList(Idlist);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public CFZS.Model.BaseDictionary GetModel(int Id)
        {

            return dal.GetModel(Id);
        }

        /// <summary>
        /// 得到一个对象实体，从缓存中
        /// </summary>
        public CFZS.Model.BaseDictionary GetModelByCache(int Id)
        {

            string CacheKey = "BaseDictionaryModel-" + Id;
            object objModel = DataCache.GetCache(CacheKey);
            if (objModel == null)
            {
                try
                {
                    objModel = dal.GetModel(Id);
                    if (objModel != null)
                    {
                        int ModelCache = ConfigHelper.GetConfigInt("ModelCache");
                        DataCache.SetCache(CacheKey, objModel, DateTime.Now.AddMinutes(ModelCache), TimeSpan.Zero);
                    }
                }
                catch { }
            }
            return (CFZS.Model.BaseDictionary)objModel;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            return dal.GetList(strWhere);
        }
        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            return dal.GetList(Top, strWhere, filedOrder);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<CFZS.Model.BaseDictionary> GetModelList(string strWhere)
        {
            DataSet ds = dal.GetList(strWhere);
            return DataTableToList(ds.Tables[0]);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<CFZS.Model.BaseDictionary> DataTableToList(DataTable dt)
        {
            List<CFZS.Model.BaseDictionary> modelList = new List<CFZS.Model.BaseDictionary>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                CFZS.Model.BaseDictionary model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = new CFZS.Model.BaseDictionary();
                    if (dt.Rows[n]["Id"] != null && dt.Rows[n]["Id"].ToString() != "")
                    {
                        model.Id = int.Parse(dt.Rows[n]["Id"].ToString());
                    }
                    if (dt.Rows[n]["ParentSysCode"] != null && dt.Rows[n]["ParentSysCode"].ToString() != "")
                    {
                        model.ParentSysCode = dt.Rows[n]["ParentSysCode"].ToString();
                    }
                    if (dt.Rows[n]["Title"] != null && dt.Rows[n]["Title"].ToString() != "")
                    {
                        model.Title = dt.Rows[n]["Title"].ToString();
                    }
                    if (dt.Rows[n]["SysCode"] != null && dt.Rows[n]["SysCode"].ToString() != "")
                    {
                        model.SysCode = dt.Rows[n]["SysCode"].ToString();
                    }
                    if (dt.Rows[n]["Value"] != null && dt.Rows[n]["Value"].ToString() != "")
                    {
                        model.Value = dt.Rows[n]["Value"].ToString();
                    }
                    if (dt.Rows[n]["Descr"] != null && dt.Rows[n]["Descr"].ToString() != "")
                    {
                        model.Descr = dt.Rows[n]["Descr"].ToString();
                    }
                    if (dt.Rows[n]["Lineno"] != null && dt.Rows[n]["Lineno"].ToString() != "")
                    {
                        model.Lineno = int.Parse(dt.Rows[n]["Lineno"].ToString());
                    }
                    if (dt.Rows[n]["CreatedAt"] != null && dt.Rows[n]["CreatedAt"].ToString() != "")
                    {
                        model.CreatedAt = DateTime.Parse(dt.Rows[n]["CreatedAt"].ToString());
                    }
                    if (dt.Rows[n]["CreatedBy"] != null && dt.Rows[n]["CreatedBy"].ToString() != "")
                    {
                        model.CreatedBy = dt.Rows[n]["CreatedBy"].ToString();
                    }
                    if (dt.Rows[n]["CreatedByName"] != null && dt.Rows[n]["CreatedByName"].ToString() != "")
                    {
                        model.CreatedByName = dt.Rows[n]["CreatedByName"].ToString();
                    }
                    if (dt.Rows[n]["UpdatedAt"] != null && dt.Rows[n]["UpdatedAt"].ToString() != "")
                    {
                        model.UpdatedAt = DateTime.Parse(dt.Rows[n]["UpdatedAt"].ToString());
                    }
                    if (dt.Rows[n]["UpdatedBy"] != null && dt.Rows[n]["UpdatedBy"].ToString() != "")
                    {
                        model.UpdatedBy = dt.Rows[n]["UpdatedBy"].ToString();
                    }
                    if (dt.Rows[n]["UpdatedByName"] != null && dt.Rows[n]["UpdatedByName"].ToString() != "")
                    {
                        model.UpdatedByName = dt.Rows[n]["UpdatedByName"].ToString();
                    }
                    modelList.Add(model);
                }
            }
            return modelList;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetAllList()
        {
            return GetList("");
        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            return dal.GetRecordCount(strWhere);
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            return dal.GetListByPage(strWhere, orderby, startIndex, endIndex);
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        //public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        //{
        //return dal.GetList(PageSize,PageIndex,strWhere);
        //}

        #endregion  Method
    }
}
